package com.cw.jeeyt.service.impl.facade;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cw.jeeyt.service.bo.facade.FacadeClassBo;
import com.cw.jeeyt.service.converter.facade.FacadeClassConverter;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.facade.FacadeClassDao;
import com.cw.jeeyt.dao.model.facade.FacadeClassDo;
import com.cw.jeeyt.dao.query.facade.FacadeClassQuery;
import com.cw.jeeyt.service.facade.FacadeClassService;
import com.cw.jeeyt.service.utils.DataDicUtils;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * 接口类接口业务实现类
 *
 * @author chenw
 * @date 2019-11-01 23:46:50
 */
@Service
public class FacadeClassServiceImpl implements FacadeClassService {
    @Autowired
    private FacadeClassDao facadeClassDao;

    /**
     * 新增接口类
     *
     * @param request 条件
     * @return String
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addFacadeClass(FacadeClassBo request) {
        if (request == null) {
            return null;
        }
        request.setCreateBy(SessionUtil.getSessionUserId());
        request.setUpdateBy(SessionUtil.getSessionUserId());
        request.setCreateDate(new Date());
        request.setUpdateDate(new Date());
        facadeClassDao.insertBySelective(FacadeClassConverter.bo2Do(request));
        return "";
    }

    /**
     * 更新接口类
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modifyFacadeClass(FacadeClassBo request) {
        if (request == null) {
            return false;
        }
        request.setUpdateBy(SessionUtil.getSessionUserId());
        request.setUpdateDate(new Date());
        facadeClassDao.updateBySelective(FacadeClassConverter.bo2Do(request));
        return true;
    }

    /**
     * 删除接口类
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteFacadeClass(FacadeClassBo request) {
        if (request == null) {
            return false;
        }
        return facadeClassDao.deleteByKey(request.getId()) > 0;
    }

    /**
     * 分页查询接口类
     *
     * @param request 条件
     * @return PageInfo<FacadeClassBo>
     */
    @Override
    public PageInfo<FacadeClassBo> selectPage(FacadeClassBo request, PageParam pageParam) throws Exception {
        PageInfo<FacadeClassDo> pageInfo = facadeClassDao.selectPage(FacadeClassQuery.builder()
            .id(request.getId())
            .classCode(request.getClassCode())
            .className(request.getClassName())
            .projectId(request.getProjectId())
            .isPublic(request.getIsPublic())
            .facadeDesc(request.getFacadeDesc())
            .createBy(request.getCreateBy())
            .createDate(request.getCreateDate())
            .updateBy(request.getUpdateBy())
            .updateDate(request.getUpdateDate())
            .build(), pageParam);
        List<FacadeClassBo> facadeClasss = FacadeClassConverter.doList2BoList(pageInfo.getList());
        if (CollectionUtil.isNotEmpty(facadeClasss)) {
            Map<String, Object> dictMap = new HashMap<>();
            dictMap.put("isPublic", "std_zb_yn");
            JSONArray jsonArray = DataDicUtils.convertDict(JSON.parseArray(JSON.toJSONString(facadeClasss)), dictMap);
            facadeClasss = JSON.parseArray(jsonArray.toJSONString(), FacadeClassBo.class);
        }
        return new PageInfo<>(pageInfo, facadeClasss);
    }

    /**
     * 查询接口类详情信息
     *
     * @param request 条件
     * @return FacadeClassBo
     */
    @Override
    public FacadeClassBo queryFacadeClassDetail(FacadeClassBo request) {
        if (request == null) {
            return null;
        }
        return FacadeClassConverter.do2Bo(facadeClassDao.selectByKey(request.getId()));
    }

    /**
     * 根据条件查询接口类
     *
     * @param request 条件
     * @return List<FacadeClassBo>
     */
    @Override
    public List<FacadeClassBo> queryFacadeClassList(FacadeClassBo request) {
        if (request == null) {
            return null;
        }
        return FacadeClassConverter.doList2BoList(facadeClassDao.selectByExample(FacadeClassQuery.builder()
            .id(request.getId())
            .classCode(request.getClassCode())
            .className(request.getClassName())
            .projectId(request.getProjectId())
            .isPublic(request.getIsPublic())
            .facadeDesc(request.getFacadeDesc())
            .createBy(request.getCreateBy())
            .createDate(request.getCreateDate())
            .updateBy(request.getUpdateBy())
            .updateDate(request.getUpdateDate())
            .build()));
    }

    /**
     * 根据key查询接口类
     *
     * @param id
     * @return
     */
    @Override
    public FacadeClassBo selectByKey(Long id) {

        return FacadeClassConverter.do2Bo(facadeClassDao.selectByKey(id));
    }
}
